package com.wyzz.hr.util;

import java.io.IOException;
import javax.servlet.*;

public class ActionFilter implements Filter {

	public ActionFilter() {
		encoding = "utf-8";
		filterConfig = null;
	}

	public void setFilterConfig(FilterConfig config) {
		filterConfig = config;
	}

	public FilterConfig getFilterConfig() {
		return filterConfig;
	}

	public void destroy() {
		encoding = null;
		filterConfig = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String encoding = selectEncoding(request);
		if (encoding != null) {
			request.setCharacterEncoding(encoding);
			response.setContentType((new StringBuilder()).append(
					"text/html;charset=").append(encoding).toString());
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		encoding = filterConfig.getInitParameter("encoding");
	}

	protected String selectEncoding(ServletRequest request) {
		return encoding;
	}

	protected String encoding;
	protected FilterConfig filterConfig;
}
